# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Christian Heusel <gromit@archlinux.org>
# Contributor: Eric Bélanger <eric@archlinux.org>

_name=Audacity
pkgbase=audacity
pkgname=(
  audacity
  audacity-docs
)
pkgver=3.4.2
pkgrel=1
epoch=1
pkgdesc="A program that lets you manipulate digital audio waveforms"
arch=(loong64 x86_64)
url="https://audacityteam.org"
license=(GPL3)
makedepends=(
  alsa-lib
  chrpath
  cmake
  ffmpeg
  flac
  gcc-libs
  gdk-pixbuf2
  glib2
  glibc
  gtk3
  gtkmm3
  jack
  lame
  libid3tag
  libmad
  libogg
  libsbsms
  libsndfile
  libsoxr
  libvorbis
  libxkbcommon-x11
  lilv
  lv2
  mpg123
  opusfile
  portaudio
  portmidi
  portsmf
  python
  rapidjson
  soundtouch
  sqlite
  suil
  twolame
  vamp-plugin-sdk
  vst3sdk
  wavpack
  wxwidgets-gtk3
  xcb-util
  xcb-util-cursor
  xcb-util-keysyms
)
source=(
  https://github.com/audacity/audacity/archive/refs/tags/$_name-$pkgver.tar.gz
  https://github.com/$pkgbase/$pkgbase/releases/download/$_name-$pkgver/$pkgbase-manual-$pkgver.zip
)
sha512sums=('a4a3bc68e46988e5538edbe6d8f42428b69e3f294b8d65337600e6806b516100d20a92135954f073c863d29767b904b781bd3a09cec40b8a85ee4463f52e1759'
            'a840409a98ba570113e445264feac22555585c9a6e64191a3d90d693f55325d9bf0a028869828f0ad3074e451087563ef47064d2774348184fe62bbd88dfde5f')
b2sums=('f5bcf6adb925c22b74d6bb1f9605e428f00d776715dd221ae2e2026ffe65a03410279f01690c9b671df9a4924940e5a93dc3535b6a41626663ccdda5ccf7554f'
        '3b49bd9ac0ceb8ee36ac8506fbec159ad6ed884ed1a1b5619a7f309c951400699fd92e1e79771bd5ca23055e2b6d24138007e613eddd8cc97ed4b443da4b5f6d')

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D AUDACITY_BUILD_LEVEL=2
    -D audacity_conan_enabled=OFF
    -D audacity_has_networking=OFF
    -D audacity_has_crashreports=OFF
    -D audacity_has_updates_check=OFF
    -D audacity_has_sentry_reporting=OFF
    -D audacity_lib_preference=system
    -D audacity_obey_system_dependencies=ON
    -D audacity_use_vst3sdk=system
    -S $pkgname-$_name-$pkgver
    -W no-dev
  )

  export VST3SDK='/usr/src/vst3sdk'
  export CFLAGS+=" -DNDEBUG"
  export CXXFLAGS+=" -DNDEBUG"
  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure
}

package_audacity() {
  license+=(BSD)
  groups=(pro-audio)
  depends=(
    alsa-lib libasound.so
    expat
    flac libFLAC.so libFLAC++.so
    gcc-libs
    glibc
    gdk-pixbuf2 libgdk_pixbuf-2.0.so
    glib2 libglib-2.0.so libgobject-2.0.so
    gtk3 libgdk-3.so libgtk-3.so
    hicolor-icon-theme
    jack  # likely dlopen'ed
    lame  # likely dlopen'ed
    libid3tag libid3tag.so
    libmad
    libogg libogg.so
    libsbsms libsbsms.so
    libsndfile libsndfile.so
    libsoxr
    libvorbis libvorbis.so libvorbisenc.so libvorbisfile.so
    libx11
    lilv liblilv-0.so
    mpg123 libmpg123.so
    opusfile
    portaudio libportaudio.so
    portmidi libportmidi.so
    portsmf libportSMF.so
    python
    rapidjson
    soundtouch
    sqlite libsqlite3.so
    suil libsuil-0.so
    twolame libtwolame.so
    util-linux-libs
    wxwidgets-gtk3
    vamp-plugin-sdk libvamp-hostsdk.so
    wavpack
    wxwidgets-common
  )
  optdepends=(
    'audacity-docs: for documentation'
    'ffmpeg: for additional import/export capabilities'
  )
  provides=(
    ladspa-host
    lv2-host
    vamp-host
    vst-host
    vst3-host
  )


  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname-$_name-$pkgver/lib-src/libnyquist/nyquist/license.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE.nyquist"
}

package_audacity-docs() {
  license+=(CCPL)
  pkgdesc+=" - documentation"

  install -vdm 755 "$pkgdir/usr/share/doc/$pkgbase/html/"
  cp -av $pkgbase-manual-$pkgver/* "$pkgdir/usr/share/doc/$pkgbase/html/"
}
